<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission, updatePermission } from '@/api/permission'
export default {
  data() {
    return {
      dialogVisible: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '' // 因为做的是树 需要知道添加到哪个节点下了
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 弹出层标题
    title() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  methods: {
    // 弹出层关闭后的操作
    handleClose(done) {
      this.formData = {}
      this.$refs.perForm.resetFields()
      this.dialogVisible = false
    },
    async submit() {
      await this.$refs.perForm.validate()
      if (this.formData.id) {
        // 编辑权限
        await updatePermission(this.formData)
      } else {
        // 新增权限
        await addPermission(this.formData)
      }
      this.$message.success('操作权限成功')
      this.$parent.getPermissionList()
      this.handleClose()
    }
  }
}
</script>

<style></style>
